import { c } from "../../tool/core";
import { cryptPwd } from "../../tool/toll";
let fs = require("fs");
import { tv_series_insert } from "../../db/insert";

export async function tv_series({ uri, dbs }) {

    function callback(res, $) {
        let nodeList = $(".item-num");
        let name: string = "";
        let url: string = "";
        let season: string = "";
        let title = "";
        let pwd = "";
        let flag: String = "0.0.0.i1"; //完整分集片源
        let prew: String = "0.0.0.i3"; //预览
        let tmpType: String = "";
        let category: String = dbs;
        let ykID = "";
        title = $("meta[name=irAlbumName]").attr("content");
        pwd = cryptPwd(title, category);
        Object.keys(nodeList).forEach((key, index) => {
            name = $(nodeList[key]).attr("title");
            season = $(nodeList[key])
                .children("a")
                .eq(0)
                .children("span")
                .eq(1)
                .text();
            tmpType = $(nodeList[key])
                .children("a")
                .eq(0)
                .children("span")
                .eq(0)
                .data("spm-anchor-id");
            url = $(nodeList[key])
                .children("a")
                .attr("href");

            //检出所有完整视频源
            if ((!tmpType || tmpType === flag) && name) {
                url = url.split("?")[0];
                ykID = url.split("id_")[1].substr(0, url.split("id_")[1].length - 5);

                let arr = [title, pwd, name, season, url, category, ykID].map(el => {
                    return `'${el}'`;
                });



                tv_series_insert(dbs, arr);
                // console.log([title, pwd, name, season, url, type]);
            }
        });

        fs.writeFileSync("./九州缥缈录.html", res, "utf-8");
    }
    c(uri, callback);

    // console.log("哈哈哈哈哈");
}
